package edu.ccut.saturn.studio.database.model;

import java.util.List;

import org.eclipse.swt.graphics.Image;


import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;

/**
 * 用于存放table或view的文件将夹
 * 
 * @author 谷钰
 * @version 1.0 2008.12.02
 */
public class SqlFolder extends SaturnTree {

	public static String TABLES = "Tables"; //$NON-NLS-1$
	public static String VIEWS = "Views"; //$NON-NLS-1$

	private SqlSchema schema = null;

	public SqlFolder(String name, SqlSchema schema) {
		super(name);

		this.schema = schema;
	}

	@Override
	public void expand() throws Exception {
		if (super.hasChildren()) {
			super.removeChildren();
		}
		
		List<String> names = null;

		if (TABLES.equals(super.getName())) {
			names = this.schema.getTableNames();

			if (names != null) {
				for (String name : names) {
					super.addChild(new SqlTable(name, this.schema));
				}
			}
		} else if (VIEWS.equals(super.getName())) {
			names = this.schema.getViewNames();

			if (names != null) {
				for (String name : names) {
					super.addChild(new SqlView(name, this.schema));
				}
			}
		}
		super.setOpen(true);
	}

	@Override
	public Image getImage() {
		if (TABLES.equals(super.getName())) {
			return Activator.getImage(Images.DATABASE_TABLE_FOLDER);
		} else if (VIEWS.equals(super.getName())) {
			return Activator.getImage(Images.DATABASE_VIEW_FOLDER);
		}

		return null;
	}

	public String getDescription() {
		StringBuffer stringbuffer = new StringBuffer();
		stringbuffer
				.append("<b>").append(super.getParent().getName()).append("<b>"); //$NON-NLS-1$ //$NON-NLS-2$
		stringbuffer.append("<p>").append(super.getName());

		return stringbuffer.toString();
	}
}